package hu.bme.aut.consulttime.client.components;

import hu.bme.aut.consulttime.client.StudentService;
import hu.bme.aut.consulttime.client.StudentServiceAsync;
import hu.bme.aut.consulttime.entitydtos.UserDTO;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class CreateStudent extends DialogBox {
	private TextBox textBoxUname;
	private TextBox textBoxPass;
	private TextBox textBoxFname;
	private TextBox textBoxLname;
	private TextBox textBoxEmail;
	private CheckBox chckbxEmail;

	/**
	 * Create a remote service proxy to talk to the server-side service.
	 */
	private StudentServiceAsync studentService = GWT
			.create(StudentService.class);

	public CreateStudent() {
		// PopupPanel's constructor takes 'auto-hide' as its boolean parameter.
		// If this is set, the panel closes itself automatically when the user
		// clicks outside of it.
		super(true);
		FlexTable flexTable = new FlexTable();

		Label lblCreateStudent = new Label("Create Student");
		flexTable.setWidget(0, 0, lblCreateStudent);

		Label lblUserName = new Label("User name (*)");
		flexTable.setWidget(1, 0, lblUserName);

		textBoxUname = new TextBox();
		flexTable.setWidget(1, 1, textBoxUname);

		Label lblPassword = new Label("Password (*)");
		flexTable.setWidget(2, 0, lblPassword);

		textBoxPass = new TextBox();
		textBoxPass.setText("");
		flexTable.setWidget(2, 1, textBoxPass);

		Label lblFirstName = new Label("First name");
		flexTable.setWidget(3, 0, lblFirstName);

		textBoxFname = new TextBox();
		textBoxFname.setText("");
		flexTable.setWidget(3, 1, textBoxFname);

		Label lblLastName = new Label("Last name");
		flexTable.setWidget(4, 0, lblLastName);

		textBoxLname = new TextBox();
		flexTable.setWidget(4, 1, textBoxLname);

		Label lblEmailAddress = new Label("Email address");
		flexTable.setWidget(5, 0, lblEmailAddress);

		textBoxEmail = new TextBox();
		textBoxEmail.setText("");
		flexTable.setWidget(5, 1, textBoxEmail);

		chckbxEmail = new CheckBox("Email notification");
		flexTable.setWidget(6, 1, chckbxEmail);

		Button btnCreate = new Button("Create Student");
		btnCreate.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
			}
		});
		flexTable.setWidget(7, 1, btnCreate);
		
		setWidget(flexTable);

		// Create a handler for the sendButton and nameField
		class MyHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				sendCreationToServer();
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					sendCreationToServer();
				}
			}

			/**
			 * Send the name from the nameField to the server and wait for a
			 * response.
			 */
			private void sendCreationToServer() {
				// First, we validate the input.
				if (textBoxUname.getText().length() == 0
						|| textBoxPass.getText().length() == 0) {
					Window.alert("Username or password is empty.");
					return;
				}

				// Then, we send the input to the server.
				// btnSignIn.setEnabled(false);

				UserDTO usertDTO = new UserDTO(textBoxUname.getText(),
						textBoxFname.getText(), textBoxLname.getText(),
						textBoxPass.getText(), textBoxEmail.getText(),
						chckbxEmail.getValue());

				studentService.createStudent(usertDTO,
						new AsyncCallback<Boolean>() {					
							@Override
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								Window.alert("Remote Procedure Call - Failure");
							}

							@Override
							public void onSuccess(Boolean result) {
								if(result == true){
									Window.confirm("'"+textBoxUname.getText() + "' user created!");
							        //Refresh the table
							        StudentPage.loadStudents();
								}else{
									Window.alert("Student creation failed!");
								}
								
							}
						});
			}
		}

		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		btnCreate.addClickHandler(handler);
	}
	
	

}
